package com.gitee.qdbp.general.system.web.controlling.handler;

import java.lang.reflect.Field;
import com.gitee.qdbp.able.beans.AcceptAttrs;
import com.gitee.qdbp.able.exception.ServiceException;
import com.gitee.qdbp.base.enums.OperateType;
import com.gitee.qdbp.base.system.model.IAccount;
import com.gitee.qdbp.base.system.model.IAccountType;
import com.gitee.qdbp.base.system.service.IDefaultValueConfig;

public class UserTypeAcceptHandler extends AcceptAttrsSimpleHandler<IAccountType> implements IDefaultValueConfig.Aware {

    private IDefaultValueConfig defaultValues;

    @Override
    public void setDefaultValueConfig(IDefaultValueConfig defaultValues) {
        this.defaultValues = defaultValues;
    }

    @Override
    protected IAccountType getUserAttrs(Object o, Field field, String dataType, OperateType operateType, IAccount user) {
        return user.getUserType();
    }

    @Override
    public AcceptAttrs<IAccountType> getCurrentAcceptAttrs(Object o, Field field, String dataType, OperateType operateType,
            IAccountType userType) throws ServiceException {

        AcceptAttrs<IAccountType> attrs = new AcceptAttrs<>();
        attrs.setRequired(true);
        attrs.setStarts(false);
        attrs.setDefaultValue(userType);
        attrs.addAcceptValue(userType);
        return attrs;
    }

    /** 取不到用户属性(一般是用户未登录)时的授权方式 **/
    @Override
    protected AcceptAttrs<IAccountType> getDefaultAcceptAttrs(Object o, Field field, String dataType,
            OperateType operateType) throws ServiceException {
        AcceptAttrs<IAccountType> attrs = new AcceptAttrs<>();
        attrs.setRequired(true);
        attrs.setDefaultValue(defaultValues.getUserType());
        attrs.addAcceptValues(defaultValues.getUserTypeChoose());
        return attrs;
    }

}
